// src/api/system.js
import request from '@/utils/request'

// ==================== 角色管理 ====================

// 获取角色列表
export const getRoleList = (params) => {
  return request({
    url: '/role',
    method: 'get',
    params
  })
}

// 获取角色详情
export const getRoleDetail = (id) => {
  return request({
    url: `/role/${id}`,
    method: 'get'
  })
}

// 创建角色
export const createRole = (data) => {
  return request({
    url: '/role',
    method: 'post',
    data
  })
}

// 更新角色
export const updateRole = (id, data) => {
  return request({
    url: `/role/${id}`,
    method: 'put',
    data
  })
}

// 删除角色
export const deleteRole = (id) => {
  return request({
    url: `/role/${id}`,
    method: 'delete'
  })
}

// 分配角色权限
export const assignRolePermissions = (roleId, permissionIds) => {
  return request({
    url: `/role/${roleId}/permissions`,
    method: 'put',
    data: { permissionIds }
  })
}

// ==================== 权限管理 ====================

// 获取权限列表
export const getPermissionList = (params) => {
  return request({
    url: '/permission',
    method: 'get',
    params
  })
}

// 获取权限树
export const getPermissionTree = () => {
  return request({
    url: '/permission/tree',
    method: 'get'
  })
}

// 获取权限详情
export const getPermissionDetail = (id) => {
  return request({
    url: `/permission/${id}`,
    method: 'get'
  })
}

// 创建权限
export const createPermission = (data) => {
  return request({
    url: '/permission',
    method: 'post',
    data
  })
}

// 更新权限
export const updatePermission = (id, data) => {
  return request({
    url: `/permission/${id}`,
    method: 'put',
    data
  })
}

// 删除权限
export const deletePermission = (id) => {
  return request({
    url: `/permission/${id}`,
    method: 'delete'
  })
}

// ==================== 用户角色管理 ====================

// 获取用户的角色
export const getUserRoles = (userId) => {
  return request({
    url: `/user/${userId}/roles`,
    method: 'get'
  })
}

// 分配用户角色
export const assignUserRoles = (userId, roleIds) => {
  return request({
    url: `/user/${userId}/roles`,
    method: 'put',
    data: { roleIds }
  })
}

// ==================== 系统信息 ====================

// 获取系统信息
export const getSystemInfo = () => {
  return request({
    url: '/system/info',
    method: 'get'
  })
}

// 获取系统统计
export const getSystemStats = () => {
  return request({
    url: '/system/stats',
    method: 'get'
  })
}